import os

import pygame
from pygame.locals import *

def load_image(name, colorkey=None):
    """Utility function to load an image with optional colorkey
    and return a pygame image object
    """
    fullname = os.path.join('assets', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', name
        raise SystemExit, message
    image = image.convert_alpha()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image


def load_sound(name):
    """Utility function to load a sound and return a pygame sound object.
    Transparently returns a dummy sound object if mixer module is not
    available
    """
    class NoneSound:
        def play(self):
            pass
    
    if not pygame.mixer:
        return NoneSound()
    fullname = os.path.join('assets', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error, message:
        print 'Cannot load sound:', fullname
        raise SystemExit, message
    return sound